package com.qf.java2105.huangchuang.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2105.huangchuang.hotel.constant.ResponseMessageConstant;
import com.qf.java2105.huangchuang.hotel.controller.BaseServlet;
import com.qf.java2105.huangchuang.hotel.entity.PageBean;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.Food;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.service.IFrontService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 前台控制层
 * @author hcvg
 * @version v1.0
 * @date 2021/09/15
 **/
@WebServlet("/front")
public class FrontController extends BaseServlet {

    private IFrontService frontService = (IFrontService) BeanFactory.getBean("frontService");

    /**
     * 通用分页查询
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String findByPageWithCondition(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String foodName = request.getParameter("foodName");
        if (StringUtils.isEmpty(foodName)){
            foodName = "%%";
        }else {
            foodName = "%" + foodName + "%";
        }
        String typeIdStr = request.getParameter("typeId");
        Integer typeId = 0;
        if (!StringUtils.isEmpty(typeIdStr)){
            typeId = Integer.valueOf(typeIdStr.trim());
        }
        String currentPage = request.getParameter("currentPage");
        if (StringUtils.isEmpty(currentPage)){
            currentPage = "1";
        }
        ResultVO<PageBean<Food>> resultVO = frontService.findByPageWithCondition(Integer.valueOf(currentPage), 6, typeId, foodName);
        request.setAttribute("pageBean", resultVO.getData());
        request.setAttribute("typeId", typeId);
        //测试能不能拿到数据
//        return JsonUtils.objectToJson(resultVO.getData());
        return ResponseMessageConstant.PREFIX_FORWARD + request.getContextPath() + "/front/detail/caidan.jsp";
    }

    /**
     * 查看菜品详细内容
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    private String showDetail(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        HttpSession session = request.getSession();
        String foodIdStr = request.getParameter("foodId");
        if (!StringUtils.isEmpty(foodIdStr)){
            Integer foodId = Integer.valueOf(foodIdStr);
            ResultVO<Food> resultVO = FOOD_SERVICE.findById(foodId);
            List<FoodType> frontTypes = (List<FoodType>) session.getAttribute("front_types");
            request.setAttribute("foodDetail", resultVO.getData());
            request.setAttribute("frontTypes", frontTypes);
            return ResponseMessageConstant.PREFIX_FORWARD + request.getContextPath() + "/front/detail/caixiangxi.jsp";
        }
        return null;
    }
}
